package com.softstao.guoyu.global;

import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.softstao.guoyu.model.DirectlyList;
import com.softstao.guoyu.model.LoginInfo;
import com.softstao.guoyu.model.me.User;
import com.softstao.guoyu.model.shop.Goods;
import com.umeng.socialize.UMAuthListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhanghongbo on 16-1-13.
 */
public class SharePreferenceManager {
    public static SharePreferenceManager sharePreference;
    private SharedPreferences mPref;
    private Gson gson;
    private String Cookie;
    private String Latitude;
    private String Longitude;
    private String orderId;
    private int agentId;
    private int rechargeId;
    private DirectlyList directlyList;
    private boolean isCancel=true;
    private String wxCode;
    private String open_id;
    private boolean OauthVerify=false;
    private UMAuthListener listener;
    private LoginInfo loginInfo;
    public static final String WX_APPID = "wxc1e43d340117062d";
    public static final String WX_APPKEY ="90f4a173e87c6af11a036228de5fd97d";
    private SharePreferenceManager(Context context) {
        mPref = context.getSharedPreferences("guoyu", Context.MODE_PRIVATE);
        gson = GsonManager.getInstance().getGson();
    }

    public static SharePreferenceManager getInstance() {
        if (sharePreference == null) {
            sharePreference = new SharePreferenceManager(GYApplication.application);
        }
        return sharePreference;
    }

    public void serUser(User user){
        if(user==null){
            mPref.edit().putString("user", null).commit();
            return;
        }
        mPref.edit().putString("user", gson.toJson(user)).commit();
    }

    public User getUser(){
        String userStr = mPref.getString("user", null);
        if(userStr==null||userStr.equals("")) {
            return null;
        }
        return gson.fromJson(userStr, User.class);
    }

    public void setGoods(Goods goods,int count){
        if (goods == null) {
            mPref.edit().putString("goods", null).commit();
            return;
        }
        String cartStr = mPref.getString("goods", null);
        List<Goods> goodses;
        if (cartStr == null) {
            goodses = new ArrayList<>();
            goodses.add(goods);
        } else {
            goodses = gson.fromJson(cartStr, new TypeToken<List<Goods>>() {}.getType());
            int i = 0;
            for (i = 0; i < goodses.size(); i++) {
                if (goods.getProductId() == goodses.get(i).getProductId()) {
                    int num = 0;
                    if (count == 0) {
                        num = goodses.get(i).getBoxNum() + goods.getBoxNum();
                    } else {
                        num = goodses.get(i).getBoxNum() + count;
                    }
                    if (num == 0) {
                        goodses.remove(i);
                    } else {
                        goodses.get(i).setBoxNum(num);
                    }
                    break;
                }
            }
            if (i == goodses.size()) {
                if (count==0) {
                    goodses.add(goods);
                }
            }
        }
        String cartJson = gson.toJson(goodses);
        mPref.edit().putString("goods", cartJson).commit();
    }

    public List<Goods> getGoods() {
        String cartStr = mPref.getString("goods", null);
        if (cartStr == null) {
            return new ArrayList<>();
        }
        List<Goods> goodses = gson.fromJson(cartStr, new TypeToken<List<Goods>>() {}.getType());
        return goodses;
    }

    public void setChooseGoods(Goods goods,int count){
        if (goods == null) {
            mPref.edit().putString("choose", null).commit();
            return;
        }
        String cartStr = mPref.getString("choose", null);
        List<Goods> goodses;
        if (cartStr == null) {
            goodses = new ArrayList<>();
            goods.setNum(1);
            goodses.add(goods);
        } else {
            goodses = gson.fromJson(cartStr, new TypeToken<List<Goods>>() {}.getType());
            int i = 0;
            for (i = 0; i < goodses.size(); i++) {
                if (goods.getProductId() == goodses.get(i).getProductId()) {
                    int num = 0;
                    num = goodses.get(i).getNum() + count;
                    if (num == 0) {
                        goodses.remove(i);
                    } else {
                        goodses.get(i).setNum(num);
                    }
                    break;
                }
            }
            if (i == goodses.size()) {
                if (count==1) {
                    goods.setNum(1);
                    goodses.add(goods);
                }
            }
        }
        String cartJson = gson.toJson(goodses);
        mPref.edit().putString("choose", cartJson).commit();
    }

    public List<Goods> getChooseGoods() {
        String cartStr = mPref.getString("choose", null);
        if (cartStr == null) {
            return new ArrayList<>();
        }
        List<Goods> goodses = gson.fromJson(cartStr, new TypeToken<List<Goods>>() {}.getType());
        return goodses;
    }

    public String getCookie() {
        return Cookie;
    }

    public void setCookie(String cookie) {
        Cookie = cookie;
    }

    public String getLongitude() {
        return Longitude;
    }

    public void setLongitude(String longitude) {
        Longitude = longitude;
    }

    public String getLatitude() {
        return Latitude;
    }

    public void setLatitude(String latitude) {
        Latitude = latitude;
    }

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public int getRechargeId() {
        return rechargeId;
    }

    public void setRechargeId(int rechargeId) {
        this.rechargeId = rechargeId;
    }

    public boolean getLogin(){
        if(mPref.getBoolean("isLogin",false)){
            return true;
        }
        else{
            return false;
        }
    }

    public void setLogin(boolean isLogin){
        mPref.edit().putBoolean("isLogin", isLogin).commit();
    }

    public boolean isFirstIn() {
        return mPref.getBoolean("isFirst",true);
    }

    public void setFirstIn(boolean firstIn) {
        mPref.edit().putBoolean("isFirst",firstIn).commit();
    }

    public boolean isCancel() {
        return isCancel;
    }

    public void setCancel(boolean cancel) {
        isCancel = cancel;
    }

    public int getAgentId() {
        if (mPref.getInt("agentId",0)==0){
            return 0;
        }else {
            return mPref.getInt("agentId",0);
        }
    }

    public void setAgentId(int agentId) {
        mPref.edit().putInt("agentId",agentId).commit();
    }

    public DirectlyList getDirectlyList() {
        return directlyList;
    }

    public void setDirectlyList(DirectlyList directlyList) {
        this.directlyList = directlyList;
    }

    public String getWxCode() {
        return wxCode;
    }

    public void setWxCode(String wxCode) {
        this.wxCode = wxCode;
    }

    public String getOpen_id() {
        return open_id;
    }

    public void setOpen_id(String open_id) {
        this.open_id = open_id;
    }

    public boolean isOauthVerify() {
        return OauthVerify;
    }

    public void setOauthVerify(boolean oauthVerify) {
        OauthVerify = oauthVerify;
    }

    public UMAuthListener getListener() {
        return listener;
    }

    public void setListener(UMAuthListener listener) {
        this.listener = listener;
    }

    public void setLoginInfo(LoginInfo loginInfo){
        if(loginInfo==null){
            mPref.edit().putString("loginInfo", null).commit();
            return;
        }
        mPref.edit().putString("loginInfo", gson.toJson(loginInfo)).commit();
    }

    public LoginInfo getLoginInfo(){
        String loginStr = mPref.getString("loginInfo", null);
        if(loginStr==null||loginStr.equals("")) {
            return null;
        }
        return gson.fromJson(loginStr, LoginInfo.class);
    }
}
